/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.StReprografia;

/**
 * Document   : StReprografiaDAO
 * 
 * Created on : 01/03/2013, 10:50:15 
 * 
 * Author     : Fiúza <rbfiuza@gmail.com>
 */ 

public class StReprografiaDAO {

    Connection conn = null;
    PreparedStatement stmt = null;
    String sql = null;

    public ArrayList<StReprografia> getReprografias() {
        ArrayList<StReprografia> stReprografiaList = new ArrayList<StReprografia>();

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "mysql");

            sql = "Select"
                    + " id"
                    + ",dsc_reprografia"
                    + ",qnt_alunos"
                    + ",qnt_copia"                    
                    + ",qnt_total"
                    + ",vlr_unit"
                    + ",vlr_final"
                    + ",indicacoes_parametros"
                + " from stReprografia where 1  = ?";

            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, 1);

            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {

                StReprografia reprografia = new StReprografia();
                reprografia.setId(rs.getInt("id"));
                reprografia.setDscReprografia(rs.getString("dsc_reprografia"));
                reprografia.setQntAlunos(rs.getInt("qnt_alunos"));
                reprografia.setQntCopias(rs.getInt("qnt_copia"));
                reprografia.setQntTotal(rs.getFloat("qnt_total"));
                reprografia.setVlrUnit(rs.getFloat("vlr_unit"));
                reprografia.setVlrFinal(rs.getFloat("vlr_final"));
                reprografia.setIndicacoesParametros(rs.getString("indicacoes_parametros"));
                
                stReprografiaList.add(reprografia);
            }

            rs.close();
            stmt.close();
            stmt = null;


            conn.close();
            conn = null;

        } catch (Exception e) {
            System.out.println(e);
        } finally {

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here           
                }

                stmt = null;
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here     
                }

                conn = null;
            }
        }

        return stReprografiaList;
    }

    public String insertReprografias() {
        String userId = null;

        return userId;
    }

    public boolean updateReprografias(String storeId, StReprografia stReprografia) {
        boolean success = true;

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "mysql");
            
            sql = "UPDATE streprografia SET "
                    + "id= ?"
                    + ",dsc_reprografia= ?"
                    + ",qnt_alunos= ?"
                    + ",qnt_copia= ?"
                    + ",qnt_total= ?"
                    + ",vlr_unit= ?"
                    + ",vlr_final= ?"
                    + ",indicacoes_parametros= ?"
                    + " WHERE id = ?";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, stReprografia.getId());
            stmt.setString(2, stReprografia.getDscReprografia());
            stmt.setInt(3, stReprografia.getQntAlunos());
            stmt.setInt(4, stReprografia.getQntCopias());
            stmt.setFloat(5, stReprografia.getQntTotal());
            stmt.setFloat(6, stReprografia.getVlrUnit());
            stmt.setFloat(7, stReprografia.getVlrFinal());
            stmt.setString(8, stReprografia.getIndicacoesParametros());
            stmt.setInt(9, stReprografia.getId());

            stmt.executeUpdate();

            stmt.close();
            stmt = null;
            conn.close();
            conn = null;

        } catch (Exception e) {

            success = false;
            System.out.println(e);

        } finally {

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here           
                }

                stmt = null;
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here     
                }

                conn = null;
            }
        }

        return success;
    }
}